package com.goodedeeds.webapp.controller;

import com.goodedeeds.domain.resource.Project;
import com.youtills.core.admin.ILookup;
import com.youtills.core.admin.LookupType;
import com.youtills.core.admin.db.dao.AdminDao;
import com.youtills.core.lang.NumberUtil;
import com.youtills.domain.contact.Address;
import com.youtills.resource.ResourceManager;
import net.sourceforge.stripes.action.RedirectResolution;
import net.sourceforge.stripes.action.Resolution;
import net.sourceforge.stripes.action.UrlBinding;

import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: rchiluve
 * Date: 2/23/14
 * Time: 8:15 PM
 * To change this template use File | Settings | File Templates.
 */
@UrlBinding(AddressAction.URL)
public class AddressAction extends BaseDashboardAction {

    public static final String URL = "/address.action";

    private static final String VIEW = JSP_ROOT + "/address-view.jsp";
    private static final String EDIT_VIEW = JSP_ROOT + "/address-edit.jsp";
    private static final String LIST_VIEW = JSP_ROOT + "/address-list.jsp";

    private Long addressId;
    private Address address;
    private Long parentResourceId;
    private Long parentResourceType;

    private String dashboardContent;

    @Override
    public String getDashboardContent() {
        return dashboardContent;
    }

    @Override
    public DashboardTab getCurrentTab() {
        return DashboardTab.NONE;
    }

    public Long getAddressId() {
        if(addressId == null) {
            addressId = NumberUtil.parseLong(getRequestParameter("projectId"));
        }
        return addressId;
    }

    public Address getAddress() {

        if(address == null) {
            if ( getAddressId() == null){
                address = ResourceManager.instance.newResource(getActionContext(), Address.class);
                address.addResourceId(getParentResourceType(), getParentResourceId());

            } else {
                address = ResourceManager.instance.getResource(getActionContext(), Address.class, getAddressId());
            }
        }
        return address;
    }

    public Long getParentResourceId() {
        if(parentResourceId == null) {
            parentResourceId = NumberUtil.parseLong(getRequestParameter("parentResourceId"));
        }
        return parentResourceId;
    }

    public Long getParentResourceType() {
        if(parentResourceType == null) {
            parentResourceType = NumberUtil.parseLong(getRequestParameter("parentResourceType"));
        }

        return parentResourceType;
    }

    public Resolution save(){
        ResourceManager.instance.saveResource(getActionContext(), getAddress());
        addressId = address.getAddressId();
        RedirectResolution res = new RedirectResolution(AddressAction.class, "view");
        res.addParameter("addressId", addressId);
        return res;
    }

    public Resolution view(){
        dashboardContent = VIEW;
        return showPage();
    }

    public Resolution edit() {
        dashboardContent = EDIT_VIEW;
        return showPage();
    }
}